//
//  ZWBChannelSecVC.m
//  Cat
//
//  Created by dllo on 15/11/30.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ZWBChannelSecVC.h"
#import "BaseHeadView.h"
#import "ZWBHeadModel.h"
@interface ZWBChannelSecVC ()
@property(nonatomic, assign)NSInteger index;
@property(nonatomic, strong)BaseHeadView *headView;
@property (nonatomic, copy) NSString *ver;
@end

@implementation ZWBChannelSecVC{
    BOOL one;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    one = YES;
    
    NSLog(@"%@", self.str);
    
    
    [self.seg addTarget:self action:@selector(whichPage:) forControlEvents:UIControlEventValueChanged];
    [self createSegm:3];
    [self createHeadView];
    
    // 刷新
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshAction)];
    self.tableView.header = header;
    
    MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshAction)];
    self.tableView.footer = footer;
    
    [self refreshAction];
}



- (void)refreshAction {
    if ([self.tableView.footer isRefreshing]) {
        NSString *URL = nil;
        if (self.index == 1) {
            URL = [NSString stringWithFormat:@"http://api.huapipi.com/show/v2/recommend/%@?deviceId=1826162&uid=1033268&w=720&h=1280&ver=%@", self.str, self.ver];
        } else if (self.index == 0) {
            URL = [NSString stringWithFormat:@"http://api.huapipi.com/show/v2/hot/%@?deviceId=1826162&uid=1033268&w=720&h=1280&ver=%@", self.str, self.ver];
        } else if (self.index == 2) {
            URL = [NSString stringWithFormat:@"http://api.huapipi.com/show/v2/latest/%@?deviceId=1826162&uid=1033268&w=720&h=1280&ver=%@", self.str, self.ver];
        }
        
        [ToolNetWorking ToolNetWorking:URL block:^(id result) {
            
            NSMutableArray *tempArr = [NSMutableArray array];
            for (NSMutableDictionary *tempDic in result[@"rows"]) {
                if (tempDic != [NSNull null]) {
                    [tempArr addObject:tempDic];
                }
            }
            
            NSMutableArray *Arr = [TableViewControllerModel BaseModel:tempArr];
            [self.arr addObjectsFromArray:Arr];
            
            // 拼接网址刷新
            HotModel *tempModel = self.arr.lastObject;
            self.ver = tempModel.ver;
            
            [self.tableView.footer endRefreshing];
            [self.tableView reloadData];
        }];
        
    }else {
        if (self.index == 1) {
            
            [ToolNetWorking ToolNetWorking:[NSString stringWithFormat:@"http://api.huapipi.com/show/v2/hot/%@?deviceId=1826162&uid=1033268&w=720&h=1280", self.str] block:^(id result) {
                
                NSMutableArray *tempArr = [NSMutableArray array];
                for (NSMutableDictionary *tempDic in result[@"rows"]) {
                    if (tempDic != [NSNull null]) {
                        [tempArr addObject:tempDic];
                    }
                }
                
                self.arr = [NSMutableArray array];
                self.arr = [TableViewControllerModel BaseModel:tempArr];
                
                // 拼接网址刷新
                HotModel *tempModel = self.arr.lastObject;
                self.ver = tempModel.ver;
                
                [self.tableView reloadData];
                [self.tableView.header endRefreshing];
            }];
        } else if (self.index == 0) {
            [ToolNetWorking ToolNetWorking:[NSString stringWithFormat:@"http://api.huapipi.com/show/v2/recommend/%@?deviceId=1826162&uid=1033268&w=720&h=1280", self.str] block:^(id result) {
                
                NSMutableArray *tempArr = [NSMutableArray array];
                for (NSMutableDictionary *tempDic in result[@"rows"]) {
                    if (tempDic != [NSNull null]) {
                        [tempArr addObject:tempDic];
                    }
                }
                
                self.arr = [NSMutableArray array];
                self.arr = [TableViewControllerModel BaseModel:tempArr];
                
                // 拼接网址刷新
                HotModel *tempModel = self.arr.lastObject;
                self.ver = tempModel.ver;
                
                [self.tableView reloadData];
                [self.tableView.header endRefreshing];
            }];
        } else if (self.index == 2) {
            [ToolNetWorking ToolNetWorking:[NSString stringWithFormat:@"http://api.huapipi.com/show/v2/latest/%@?deviceId=1826162&uid=1033268&w=720&h=1280", self.str] block:^(id result) {
                
                NSMutableArray *tempArr = [NSMutableArray array];
                for (NSMutableDictionary *tempDic in result[@"rows"]) {
                    if (tempDic != [NSNull null]) {
                        [tempArr addObject:tempDic];
                    }
                }
                
                self.arr = [NSMutableArray array];
                self.arr = [TableViewControllerModel BaseModel:tempArr];
                
                // 拼接网址刷新
                HotModel *tempModel = self.arr.lastObject;
                self.ver = tempModel.ver;
                [self.tableView reloadData];
                [self.tableView.header endRefreshing];
            }];
        }
    }
}



- (void)createHeadView {
    if (one) {
        self.headView = [[BaseHeadView alloc] init];
    }
    self.headView.descrLabel.numberOfLines = 0;
    self.headView.descrLabel.frame = CGRectMake(5, VIEW_HEIGHT / 4 + 45, VIEW_WIDTH-10, 0);
    self.headView.descrLabel.font = [UIFont systemFontOfSize:15];
    [self.headView.descrLabel sizeToFit];
    CGFloat hei = self.headView.descrLabel.frame.size.height;
    self.headView.frame = CGRectMake(0, 0, VIEW_WIDTH, VIEW_HEIGHT / 4 + 45 + hei);
    self.tableView.tableHeaderView = self.headView;
    
    [ToolNetWorking ToolNetWorking:[NSString stringWithFormat:@"http://api.huapipi.com/channel/descr/%@?deviceId=1826162&uid=1033268", self.str] block:^(id result) {
        NSArray *arr = [NSMutableArray arrayWithObject:result[@"channel"]];
        NSArray *secArr = [ZWBHeadModel BaseModel:arr];
        ZWBHeadModel *model = secArr[0];
        
        [self.headView.myImageView sd_setImageWithURL:[NSURL URLWithString:model.bannerList[0][@"picUrl"]]placeholderImage:[UIImage imageNamed:@"ooopic_1445565008@2x"]];
        self.headView.titleLabel.text = model.channelName;
        self.headView.commentsLabel.text = [NSString stringWithFormat:@"%@人关注", model.fans];
        self.headView.descrLabel.text = model.descr;
        if (one) {
            one = NO;
        [self createHeadView];
        }
        [self.tableView reloadData];
    }];
}



- (void)whichPage:(UISegmentedControl *)seg {
    if (seg.selectedSegmentIndex == 0) {
        self.index = 0;
        [self refreshAction];
        
    }else if (seg.selectedSegmentIndex == 1) {
        self.index = 1;
        
        [self refreshAction];    }else {
        self.index = 2;
        [self refreshAction];
    }
    
    [self.tableView setContentOffset:CGPointMake(0, -64) animated:NO
     ];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = NO;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
